/************************************************************************/
/*    Author: Scott Bevin 
-Chris i added a Read in properties function here, its based on your original code from
Game entity factory, i needed to do a similar thing and i figured this was a good place to put the
re-usable function feel free to move it*/
/************************************************************************/

#pragma once

#include <string>
#include <vector>
#include <map>
#include <stack>
#include <sstream>

#include "../XDelegates/Delegate.h"
#include "../XPropertys/PropertyContainer.h"
#include "Utilities.h"
#include "../XPropertys/PropertyFactory.h"

namespace XF
{
	class ScriptParser
	{
	public:
		typedef Delegate2<void, std::string, std::vector<std::string>& > ConfigScriptCallback;

	private:
		std::stack<std::string> mOpenFiles;
		ConfigScriptCallback mCallbackFunction;

		std::vector<std::string> mLines;

		std::map<std::string, std::string> mTypedefMap;
		std::map<std::string, std::string> mVariableMap;

		std::string ResolveFileName(std::string inputFileName);

		bool SeperateLine(std::string line, std::vector<std::string> &outStrings);

		bool ParseSystemCommand(std::string line);
		bool ParseLine(std::string line);
		bool ParseAllLines();
		bool ReadFile(std::string fileName);

	public:
		static bool ReadInProperties(XF::PropertyContainer* owner, std::string properties, std::string seperator = ":")
		{
			std::vector<std::string> theProperties;
			XF::Utilities::TokenizeString(properties, theProperties, seperator);

			for(unsigned int i = 0; i < theProperties.size(); ++i)
			{
				std::vector<std::string> tokens;
				XF::Utilities::TokenizeString(theProperties[i], tokens);

				IProperty* newProp = PropertyFactory::CreateProperty(tokens[0], tokens[1]);

				std::string value = "";

				for(unsigned int i = 2; i < tokens.size(); ++i) value += tokens[i] + " ";
				value = value.substr(0, value.size() - 1);


				//value = XF::Utilities::ToLower(value);
				newProp->SetFromString(value);
				owner->AddProperty(newProp);
			}
			return true;
		}

	
		ScriptParser();
		~ScriptParser();

		bool ParseFile(std::string fileName, ConfigScriptCallback callbackFunction);

		void RegisterTypedef(std::string key, std::string value);
	};
}
